//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/slider.h>
#include <wx/gbsizer.h>
#include "levelchooser.h"
#include "statusctrl.h"

enum cSliderID
{
	cSliderIDWarning=11000,
	cSliderIDError
};

BEGIN_EVENT_TABLE(CLevelChooser, wxPanel)
	EVT_SCROLL(CLevelChooser::OnSlider)
END_EVENT_TABLE()



CLevelChooser::CLevelChooser(wxWindow* pParent):
	wxPanel(pParent, wxID_ANY)
{
	wxGridBagSizer *pGrid = new wxGridBagSizer(10, 10);
	 	
 	wxStaticText *pWarning 	= new wxStaticText(this, wxID_ANY, _("Warning(%):"));
 	wxStaticText *pAlarm   	= new wxStaticText(this, wxID_ANY, _("Alarm(%):"));
 	wxStaticText *pPrev		= new wxStaticText(this, wxID_ANY, _("Preview"));
 	
 	pGrid->Add(pWarning, 	wxGBPosition(0,0), wxDefaultSpan, wxALL|wxALIGN_CENTER);
 	pGrid->Add(pAlarm, 		wxGBPosition(0,1), wxDefaultSpan, wxALL|wxALIGN_CENTER);
 	pGrid->Add(pPrev, 		wxGBPosition(0,2), wxDefaultSpan, wxALL|wxALIGN_CENTER);

	m_pcWarning	= new wxSlider(this, 	cSliderIDWarning, 	50, 0, 100, wxDefaultPosition, wxSize(STATUS_CTRL_WIDTH, 200), wxSL_INVERSE|wxSL_VERTICAL|wxSL_LABELS );
	m_pcAlarm  	= new wxSlider(this, 	cSliderIDError, 	50, 0, 100, wxDefaultPosition, wxSize(STATUS_CTRL_WIDTH, 200), wxSL_INVERSE|wxSL_VERTICAL|wxSL_LABELS );
	m_pStatus 	= new CStatusCtrl(this, wxID_ANY, 			wxDefaultPosition, wxSize(STATUS_CTRL_WIDTH, 200));
	m_pStatus->SetLevel(100);

 	pGrid->Add(m_pcWarning, wxGBPosition(1, 0), wxDefaultSpan, wxEXPAND, 10 );
 	pGrid->Add(m_pcAlarm,	wxGBPosition(1, 1), wxDefaultSpan, wxEXPAND, 10 );
	pGrid->Add(m_pStatus, 	wxGBPosition(1, 2), wxDefaultSpan, wxEXPAND, 10 );	

	SetSizer( pGrid );
}

CLevelChooser::~CLevelChooser()
{
}

void CLevelChooser::OnSlider(wxScrollEvent& event)
{
	int nWarning= m_pcWarning->GetValue();
	int nAlarm	= m_pcAlarm->GetValue();
	
	if ( nWarning>nAlarm )
		m_pcAlarm->SetValue(nWarning);
	
	m_pStatus->SetWarningLevel((unsigned char)nWarning);
	m_pStatus->SetAlarmLevel((unsigned char)nAlarm);
	
	//Save values
	m_btWarning = (unsigned char)nWarning;
	m_btAlarm = (unsigned char)nAlarm;
}

unsigned char CLevelChooser::GetWarningLevel()
{
	return (unsigned char)m_btWarning;
}

void CLevelChooser::SetWarningLevel(unsigned char val)
{
	m_btWarning = val;
	m_pcWarning->SetValue(m_btWarning);
}

unsigned char CLevelChooser::GetAlarmLevel()
{
	return (unsigned char)m_btAlarm;
}

void CLevelChooser::SetAlarmLevel(unsigned char val)
{
	m_btAlarm = val;
	m_pcAlarm->SetValue(val);
}
